<template>
  <div class="booking-page">
    <div class="container">
      <div class="booking-content">
        <!-- 预订步骤 -->
        <div class="booking-steps">
          <el-steps :active="currentStep" finish-status="success">
            <el-step title="选择日期" />
            <el-step title="填写信息" />
            <el-step title="确认支付" />
            <el-step title="预订成功" />
          </el-steps>
        </div>

        <div class="booking-main">
          <!-- 左侧内容 -->
          <div class="booking-form-section">
            <!-- 步骤1: 选择日期 -->
            <div v-if="currentStep === 0" class="step-content">
              <h2>选择入住日期</h2>
              <div class="date-selection">
                <div class="date-picker-container">
                  <el-date-picker
                    v-model="bookingData.checkIn"
                    type="date"
                    placeholder="入住日期"
                    size="large"
                    :disabled-date="disabledDate"
                    @change="handleDateChange"
                  />
                  <el-date-picker
                    v-model="bookingData.checkOut"
                    type="date"
                    placeholder="退房日期"
                    size="large"
                    :disabled-date="disabledDate"
                    @change="handleDateChange"
                  />
                </div>
                <div class="guests-selection">
                  <h3>入住人数</h3>
                  <el-select v-model="bookingData.guests" placeholder="选择人数" size="large">
                    <el-option label="1人" value="1" />
                    <el-option label="2人" value="2" />
                    <el-option label="3人" value="3" />
                    <el-option label="4人" value="4" />
                    <el-option label="5人以上" value="5+" />
                  </el-select>
                </div>
              </div>
            </div>

            <!-- 步骤2: 填写信息 -->
            <div v-if="currentStep === 1" class="step-content">
              <h2>填写预订信息</h2>
              <el-form :model="bookingData" :rules="formRules" ref="bookingForm" label-width="100px">
                <el-form-item label="姓名" prop="name">
                  <el-input v-model="bookingData.name" placeholder="请输入您的姓名" />
                </el-form-item>
                <el-form-item label="手机号" prop="phone">
                  <el-input v-model="bookingData.phone" placeholder="请输入手机号" />
                </el-form-item>
                <el-form-item label="身份证号" prop="idCard">
                  <el-input v-model="bookingData.idCard" placeholder="请输入身份证号" />
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                  <el-input v-model="bookingData.email" placeholder="请输入邮箱地址" />
                </el-form-item>
                <el-form-item label="特殊需求">
                  <el-input
                    v-model="bookingData.specialRequests"
                    type="textarea"
                    :rows="3"
                    placeholder="如有特殊需求请在此说明"
                  />
                </el-form-item>
              </el-form>
            </div>

            <!-- 步骤3: 确认支付 -->
            <div v-if="currentStep === 2" class="step-content">
              <h2>确认支付</h2>
              <div class="payment-methods">
                <h3>选择支付方式</h3>
                <el-radio-group v-model="bookingData.paymentMethod">
                  <div class="payment-option">
                    <el-radio value="wechat">
                      <div class="payment-item">
                        <el-icon><ChatDotRound /></el-icon>
                        <span>微信支付</span>
                      </div>
                    </el-radio>
                  </div>
                  <div class="payment-option">
                    <el-radio value="alipay">
                      <div class="payment-item">
                        <el-icon><Money /></el-icon>
                        <span>支付宝</span>
                      </div>
                    </el-radio>
                  </div>
                  <div class="payment-option">
                    <el-radio value="card">
                      <div class="payment-item">
                        <el-icon><CreditCard /></el-icon>
                        <span>银行卡</span>
                      </div>
                    </el-radio>
                  </div>
                </el-radio-group>
              </div>
              
              <div class="agreement">
                <el-checkbox v-model="bookingData.agreedToTerms">
                  我已阅读并同意
                  <el-link type="primary" @click="showTerms">《预订条款》</el-link>
                  和
                  <el-link type="primary" @click="showPrivacy">《隐私政策》</el-link>
                </el-checkbox>
              </div>
            </div>

            <!-- 步骤4: 预订成功 -->
            <div v-if="currentStep === 3" class="step-content success-content">
              <div class="success-icon">
                <el-icon><CircleCheck /></el-icon>
              </div>
              <h2>预订成功！</h2>
              <p>您的预订已确认，我们将尽快与您联系</p>
              <div class="success-actions">
                <el-button type="primary" @click="$router.push('/orders')">
                  查看订单
                </el-button>
                <el-button @click="$router.push('/')">
                  返回首页
                </el-button>
              </div>
            </div>

            <!-- 操作按钮 -->
            <div class="step-actions">
              <el-button v-if="currentStep > 0" @click="prevStep">
                上一步
              </el-button>
              <el-button
                v-if="currentStep < 3"
                type="primary"
                @click="nextStep"
                :disabled="!canProceed"
              >
                {{ currentStep === 2 ? '确认支付' : '下一步' }}
              </el-button>
            </div>
          </div>

          <!-- 右侧房源信息 -->
          <div class="property-summary">
            <div class="property-card">
              <div class="property-image">
                <img :src="property.image" :alt="property.title" />
              </div>
              <div class="property-info">
                <h3>{{ property.title }}</h3>
                <p class="property-location">
                  <el-icon><Location /></el-icon>
                  {{ property.location }}
                </p>
                <div class="property-rating">
                  <el-rate v-model="property.rating" disabled show-score />
                </div>
              </div>
            </div>

            <!-- 价格详情 -->
            <div class="price-details">
              <h3>价格详情</h3>
              <div class="price-item">
                <span>¥{{ property.price }} × {{ nights }}晚</span>
                <span>¥{{ property.price * nights }}</span>
              </div>
              <div class="price-item">
                <span>服务费</span>
                <span>¥{{ serviceFee }}</span>
              </div>
              <div class="price-item">
                <span>清洁费</span>
                <span>¥{{ cleaningFee }}</span>
              </div>
              <div class="price-item total">
                <span>总计</span>
                <span>¥{{ totalPrice }}</span>
              </div>
            </div>

            <!-- 预订摘要 -->
            <div class="booking-summary">
              <h3>预订摘要</h3>
              <div class="summary-item">
                <span>入住日期</span>
                <span>{{ formatDate(bookingData.checkIn) }}</span>
              </div>
              <div class="summary-item">
                <span>退房日期</span>
                <span>{{ formatDate(bookingData.checkOut) }}</span>
              </div>
              <div class="summary-item">
                <span>入住人数</span>
                <span>{{ bookingData.guests }}人</span>
              </div>
              <div class="summary-item">
                <span>入住天数</span>
                <span>{{ nights }}晚</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'

const route = useRoute()
const router = useRouter()

// 当前步骤
const currentStep = ref(0)

// 预订数据
const bookingData = reactive({
  checkIn: '',
  checkOut: '',
  guests: '',
  name: '',
  phone: '',
  idCard: '',
  email: '',
  specialRequests: '',
  paymentMethod: 'wechat',
  agreedToTerms: false
})

// 表单验证规则
const formRules = {
  name: [
    { required: true, message: '请输入姓名', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  idCard: [
    { required: true, message: '请输入身份证号', trigger: 'blur' },
    { pattern: /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/, message: '请输入正确的身份证号', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ]
}

// 房源信息
const property = ref({
  id: 1,
  title: '温馨小院民宿',
  location: '北京·朝阳区',
  price: 299,
  rating: 4.8,
  image: 'https://images.unsplash.com/photo-1566073771259-6a8506099945?w=400&h=300&fit=crop'
})

// 费用计算
const nights = computed(() => {
  if (bookingData.checkIn && bookingData.checkOut) {
    const checkIn = new Date(bookingData.checkIn)
    const checkOut = new Date(bookingData.checkOut)
    const diffTime = Math.abs(checkOut - checkIn)
    return Math.ceil(diffTime / (1000 * 60 * 60 * 24))
  }
  return 0
})

const serviceFee = computed(() => {
  return Math.round(property.value.price * nights.value * 0.1)
})

const cleaningFee = computed(() => {
  return 50
})

const totalPrice = computed(() => {
  return property.value.price * nights.value + serviceFee.value + cleaningFee.value
})

// 是否可以继续下一步
const canProceed = computed(() => {
  switch (currentStep.value) {
    case 0:
      return bookingData.checkIn && bookingData.checkOut && bookingData.guests
    case 1:
      return bookingData.name && bookingData.phone && bookingData.idCard && bookingData.email
    case 2:
      return bookingData.paymentMethod && bookingData.agreedToTerms
    default:
      return true
  }
})

// 初始化
onMounted(() => {
  // 从路由参数获取预订信息
  if (route.query.checkIn) {
    bookingData.checkIn = route.query.checkIn
  }
  if (route.query.checkOut) {
    bookingData.checkOut = route.query.checkOut
  }
  if (route.query.guests) {
    bookingData.guests = route.query.guests
  }
})

// 禁用日期（只能选择今天之后的日期）
const disabledDate = (time) => {
  return time.getTime() < Date.now() - 8.64e7
}

// 处理日期变化
const handleDateChange = () => {
  // 可以在这里添加日期验证逻辑
}

// 下一步
const nextStep = async () => {
  if (currentStep.value === 1) {
    // 验证表单
    const form = document.querySelector('.el-form')
    if (form) {
      const valid = await form.validate()
      if (!valid) return
    }
  }
  
  if (currentStep.value === 2) {
    // 处理支付
    await handlePayment()
  }
  
  currentStep.value++
}

// 上一步
const prevStep = () => {
  currentStep.value--
}

// 处理支付
const handlePayment = async () => {
  // 模拟支付处理
  console.log('处理支付:', bookingData.paymentMethod)
  
  // 这里可以调用支付API
  await new Promise(resolve => setTimeout(resolve, 2000))
  
  console.log('支付成功')
}

// 格式化日期
const formatDate = (date) => {
  if (!date) return ''
  return new Date(date).toLocaleDateString('zh-CN')
}

// 显示条款
const showTerms = () => {
  console.log('显示预订条款')
}

// 显示隐私政策
const showPrivacy = () => {
  console.log('显示隐私政策')
}
</script>

<style scoped>
.booking-page {
  min-height: 100vh;
  background: #f8f9fa;
  padding: 20px 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.booking-steps {
  background: white;
  padding: 30px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  margin-bottom: 30px;
}

.booking-main {
  display: grid;
  grid-template-columns: 1fr 400px;
  gap: 30px;
}

.booking-form-section {
  background: white;
  border-radius: 12px;
  padding: 30px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.step-content h2 {
  font-size: 1.8rem;
  color: #333;
  margin-bottom: 30px;
}

.date-selection {
  space-y: 30px;
}

.date-picker-container {
  display: flex;
  gap: 20px;
  margin-bottom: 30px;
}

.date-picker-container .el-date-editor {
  flex: 1;
}

.guests-selection h3 {
  font-size: 1.2rem;
  color: #333;
  margin-bottom: 15px;
}

.step-actions {
  display: flex;
  justify-content: space-between;
  margin-top: 40px;
  padding-top: 30px;
  border-top: 1px solid #eee;
}

.payment-methods h3 {
  font-size: 1.2rem;
  color: #333;
  margin-bottom: 20px;
}

.payment-option {
  margin-bottom: 15px;
}

.payment-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 15px;
  border: 1px solid #ddd;
  border-radius: 8px;
  transition: all 0.3s;
}

.payment-item:hover {
  border-color: #409eff;
}

.payment-item .el-icon {
  font-size: 20px;
  color: #409eff;
}

.agreement {
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid #eee;
}

.success-content {
  text-align: center;
  padding: 60px 30px;
}

.success-icon {
  margin-bottom: 30px;
}

.success-icon .el-icon {
  font-size: 80px;
  color: #67c23a;
}

.success-content h2 {
  color: #67c23a;
  margin-bottom: 15px;
}

.success-content p {
  color: #666;
  font-size: 1.1rem;
  margin-bottom: 40px;
}

.success-actions {
  display: flex;
  gap: 20px;
  justify-content: center;
}

.property-summary {
  space-y: 20px;
}

.property-card {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.property-image {
  height: 200px;
  overflow: hidden;
}

.property-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.property-info {
  padding: 20px;
}

.property-info h3 {
  font-size: 1.3rem;
  color: #333;
  margin-bottom: 10px;
}

.property-location {
  color: #666;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 5px;
}

.price-details,
.booking-summary {
  background: white;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.price-details h3,
.booking-summary h3 {
  font-size: 1.2rem;
  color: #333;
  margin-bottom: 20px;
}

.price-item,
.summary-item {
  display: flex;
  justify-content: space-between;
  margin-bottom: 15px;
  color: #666;
}

.price-item.total {
  font-weight: bold;
  color: #333;
  font-size: 1.1rem;
  padding-top: 15px;
  border-top: 1px solid #eee;
}

@media (max-width: 768px) {
  .booking-main {
    grid-template-columns: 1fr;
  }
  
  .date-picker-container {
    flex-direction: column;
  }
  
  .step-actions {
    flex-direction: column;
    gap: 15px;
  }
  
  .success-actions {
    flex-direction: column;
  }
}
</style>


